package org.jeecg.access.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.access.constant.PlatformType;
import org.jeecg.access.entity.PlatformTotal;
import org.jeecg.access.platform.entity.WxDetailsData;
import org.jeecg.access.platform.entity.WxArticleTotal;
import org.jeecg.access.platform.mapper.WxCountMapper;
import org.jeecg.access.service.UnifiedPlatformService;
import org.jeecg.common.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Supplier;

/**
 * @description: 微信平台数据统计服务实现
 * @author: taogang
 * @date: 2025/7/15 12:20
 */
@Slf4j
@Service
public class WxCountPlatformServiceImpl implements UnifiedPlatformService {
    private final WxCountMapper wxCountMapper;

    public WxCountPlatformServiceImpl(WxCountMapper wxCountMapper) {
        this.wxCountMapper = wxCountMapper;
    }

    @Override
    public PlatformTotal getAccountTotal() {
        try {
            PlatformTotal platformTotal = wxCountMapper.getAccountNum();
            if (platformTotal == null) {
                platformTotal = new PlatformTotal();
            }
            platformTotal.setPlatformSign(PlatformType.WECHAT.getCode());
            return platformTotal;
        } catch (Exception e) {
            log.error("获取微信账号统计数据失败", e);
            return new PlatformTotal();
        }
    }

    @Override
    public Object getPlatformTotal() {
        try {
            WxDetailsData result = wxCountMapper.getPlatformTotal();
            return result != null ? result : new WxDetailsData();
        } catch (Exception e) {
            log.error("获取微信平台总计数据失败", e);
            return new WxDetailsData();
        }
    }

    @Override
    public List<WxDetailsData> getPlatformDetailData() {
        List<WxDetailsData> details = safeQuery(wxCountMapper::getPlatformDetails);
        List<WxArticleTotal> articles = safeQuery(wxCountMapper::getArticleTotalData);

        if (!details.isEmpty() && !articles.isEmpty()) {
            associateData(details, articles);
        }

        return details;
    }

    // 安全查询方法
    private <T> List<T> safeQuery(Supplier<List<T>> query) {
        try {
            List<T> result = query.get();
            return result != null ? result : new ArrayList<>();
        } catch (Exception e) {
            log.error("查询失败", e);
            return new ArrayList<>();
        }
    }

    // 关联微信数据
    private void associateData(List<WxDetailsData> details, List<WxArticleTotal> articles) {
        // 创建loginId到文章列表的映射
        Map<String, List<WxArticleTotal>> articleMap = new HashMap<>();
        for (WxArticleTotal article : articles) {
            if (article != null && article.getLoginId() != null) {
                // 设置UUID
                article.setId(UUIDGenerator.generate());

                String loginId = article.getLoginId();
                List<WxArticleTotal> list = articleMap.get(loginId);
                if (list == null) {
                    list = new ArrayList<>();
                    articleMap.put(loginId, list);
                }
                list.add(article);
            }
        }

        // 关联文章到详情数据
        for (WxDetailsData detail : details) {
            if (detail != null && detail.getId() != null) {
                List<WxArticleTotal> artList = articleMap.get(detail.getId());
                detail.setTotalDataList(artList != null ? artList : new ArrayList<>());
            }
        }
    }

    @Override
    public String getPlatformTypeCode() {
        return PlatformType.WECHAT.getCode();
    }
}